package com.lixianhe.onlineexam.controller;

import com.lixianhe.onlineexam.pojo.Message;
import com.lixianhe.onlineexam.service.MessageService;
import com.lixianhe.onlineexam.util.ApiResultHandler;
import com.lixianhe.onlineexam.util.PageBean;
import com.lixianhe.onlineexam.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    @PostMapping("message")
    public R addMessage(@RequestBody Message message) {
        int res = messageService.addMessage(message);
        if (res == 1) return ApiResultHandler.buildApiResult(200, "添加留言成功", res);
        return ApiResultHandler.buildApiResult(400, "添加留言失败", res);

    }

    @GetMapping("messages/{page}/{size}")
    public R messages(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageBean<Message> messagePageBean = messageService.MessageByPage(page, size);
        return ApiResultHandler.buildApiResult(200, "分页查找成功", messagePageBean);
    }

    @GetMapping("/message/{id}")
    public R selectById(@PathVariable("id") Integer id) {
        Message message = messageService.selectById(id);
        return ApiResultHandler.buildApiResult(200, "查询消息", message);
    }
}
